package com.roy.netty.pojoTransfer;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author ：楼兰
 * @date ：Created in 2021/6/8
 * @description: 传递POJO类型的消息
 **/

public class PojoNettyClient {
    public static void main(String[] args) {
        String host = "127.0.0.1";
        int port = 8089;
        final EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            final ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new PojoDecoder());
                            pipeline.addLast(new PojoEncoder());
                            pipeline.addLast(new PojoNettyClientHandler());
                        }
                    });
            final ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
            System.out.println("client is ready");
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }

    }
}
